//
//  YDFMDB.m
//  YDBASE
//
//  Created by wangyd on 2021/12/22.
//  Copyright © 2021 wuyezhiguhun. All rights reserved.
//

#import <FMDB/FMDB.h>

#import "YDFMDB.h"

@interface YDFMDB()

@property (nonatomic, copy) NSString *databasePath;

@end

@implementation YDFMDB


/**
 * 创建数据库
 */
- (void)newFMDBDatabase {
    
    //获得数据库文件的路径
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString *databasePath = [documentPath stringByAppendingPathComponent:@"FMDBDatabase.sqlite"];
    
    
    FMDatabase *ydDatabase = [FMDatabase databaseWithPath:self.databasePath];
    if ([ydDatabase open]) {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 打开数据库");
        
        [ydDatabase close];
    } else {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库打开失败");
    }
}

/**
 * 创建表单
 * @pram tableName 表名称
 '*/
- (void)newFMDBDatabaseCreateTableWithName:(NSString *)tableName {
    FMDatabase *ydDatabase = [FMDatabase databaseWithPath:self.databasePath];
    if ([ydDatabase open]) {
        //CREATE TABLE '%@' ('user_id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'user_name' VARCHAR(255),'user_age' VARCHAR(30))
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE '%@' ('user_id' INTEGER PRIMARY KEY NOT NULL, 'user_name' VARCHAR(255),'user_age' VARCHAR(255))", tableName];
        BOOL res = [ydDatabase executeUpdate:sql];
        if (res) {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 创建数据库表 =>: 成功");
        } else {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 创建数据库表 =>: 失败");
        }
        [ydDatabase close];
    } else {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库打开失败");
    }
}

/**
 * 插入数据
 * @pram name 插入人姓名
 * @pram age 插入人年纪
 */
- (void)insertIntoFMDBDatabaseWithName:(NSString *)name age:(NSString *)age {
    
    NSInteger userId = arc4random() % 100000 + 1;
    NSString *sql = [NSString stringWithFormat:@"insert into user(user_id,user_name,user_age) values('%ld','%@','%@')", userId, name, age];
    
    FMDatabase *ydDatabase = [FMDatabase databaseWithPath:self.databasePath];
    if ([ydDatabase open]) {
        BOOL res = [ydDatabase executeUpdate:sql];
        if (res) {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 插入数据 =>: 成功");
        } else {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 插入数据 =>: 失败");
        }
        [ydDatabase close];
    } else {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库打开失败");
    }
    
    
}

/**
 * 查询数据 查询一个表的全部数据
 */
- (void)selectFMDBDatabaseWithTableAll {
    NSString *sql = @"select * from user";
    FMDatabase *ydDatabase = [FMDatabase databaseWithPath:self.databasePath];
    
    if ([ydDatabase open]) {
        FMResultSet *resultSet = [ydDatabase executeQuery:sql];
        
        while ([resultSet next]) {
            NSInteger userId = [resultSet intForColumn:@"user_id"];
            NSString *userName = [resultSet stringForColumn:@"user_name"];
            NSString *userAge = [resultSet stringForColumn:@"user_age"];
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据查询 All =>: id：%ld   name：%@   age：%@",userId, userName, userAge);
            
        }
        
    } else {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库打开失败");
    }
    
}

/**
 * 改数据库
 * @pram userName 修改的名字
 */
- (void)updateFMDBDatabaseWithUserName:(NSString *)userName {
    
    NSString *sql = [NSString stringWithFormat:@"update user set user_name = '%@' where user_id = '51055'", userName];
    
    FMDatabase *ydDatabase = [FMDatabase databaseWithPath:self.databasePath];
    
    if ([ydDatabase open]) {
        BOOL res = [ydDatabase executeUpdate:sql];
        if (res) {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 修改数据 =>: 成功");
        } else {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 修改数据 =>: 失败");
        }
        [ydDatabase close];
    } else {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库打开失败");
    }
}

/**
 * 删除数据库
 * @pram userName 要删除数据的name
 */
- (void)deleteFMDBDatabaseWithUserName:(NSString *)userName {
    
    NSString *sql = [NSString stringWithFormat:@"delete from user where user_name = '%@'", userName];
    
    FMDatabase *ydDatabase = [FMDatabase databaseWithPath:self.databasePath];
    if ([ydDatabase open]) {
        
        BOOL res = [ydDatabase executeUpdate:sql];
        
        if (res) {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 删除数据 =>: 成功");
        } else {
            NSLog(@"wuyezhiguhun =>: YDFMDB =>: 删除数据 =>: 失败");
        }
        
        [ydDatabase close];
    } else {
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库打开失败");
    }
    
}

- (NSString *)databasePath {
    if (!_databasePath) {
        //获得数据库文件的路径
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        _databasePath = [documentPath stringByAppendingPathComponent:@"FMDBDatabase.sqlite"];
        NSLog(@"wuyezhiguhun =>: YDFMDB =>: 数据库地址 =>: %@", _databasePath);
    }
    return _databasePath;
}

@end
